package ibmetp.mis.controller.bulletin;

import ibmetp.mis.common.Constants;
import ibmetp.mis.json.DateJsonValueProcessor;
import ibmetp.mis.service.PageService;
import ibmetp.mis.serviceImpl.BulletinServiceImpl;
import ibmetp.mis.vo.Bulletin;
import ibmetp.mis.vo.Page;
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

public class BulletinPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BulletinPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	     request.setCharacterEncoding(Constants.ENCODING_UTF8);
			
			//当前页号
			int pageNo=request.getParameter("page")!=null?Integer.parseInt(request.getParameter("page")):1;
			System.out.println("pageNo:"+pageNo);
			
			//页面大小
			int pageSize=request.getParameter("rows")!=null?Integer.parseInt(request.getParameter("rows")):2;
			System.out.println("pageSize:"+pageSize);
			
			//查询条件Bulletin
			Bulletin bulletin=new Bulletin();
		
			bulletin.setPublishMsg(request.getParameter("publishMsg"));
			bulletin.setPrincipal(request.getParameter("prId"));
			
			
			//查询数据
			PageService us=new BulletinServiceImpl();
			Page p=us.getPage(pageNo, pageSize, bulletin);	   
			
			//输出JSON数据
			
			
			JSONObject o=new JSONObject();
			//若对象中含有日期对象采用
			 JsonConfig jsonConfig = new JsonConfig();  
		     jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));  
		   //若无的话：o.put("rows",p.getList());		
			o.put("rows",JSONSerializer.toJSON(p.getList() , jsonConfig).toString());
			o.put("total",p.getTotal());
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().write(o.toString());
		  
		
	}

}